<?php

/**
 * 这是附件模型 "{{attachment}}".
 *
 * The followings are the available columns in table '{{attachment}}':
 * @property integer $zat_id 附件ID
 * @property integer $zm_id  会员ID
 * @property string $zat_file_name  文件名
 * @property string $zat_url 文件地址
 * @property string $zat_title 说明
 * @property string $zat_description 文件描述
 * @property string $zat_file_type 文件类型
 * @property integer $zat_file_size 文件大小
 * @property string $zat_thumb 文件缩略图
 * @property integer $zat_image 是否图片：1否2是
 * @property string $zat_ip 上传IP
 * @property integer $zat_status 状态：1有效2无效
 * @property integer $iputtime 创建时间
 * @property integer $updatetime 更新时间
 */
class Attachment extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Attachment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{attachment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zat_file_size, zat_image, zat_status, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zat_file_name, zat_title, zat_thumb', 'length', 'max'=>100),
			array('zat_url', 'length', 'max'=>255),
			array('zat_file_type', 'length', 'max'=>50),
			array('zat_ip', 'length', 'max'=>25),
			array('zat_description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zat_id, zm_id, zat_file_name, zat_url, zat_title, zat_description, zat_file_type, zat_file_size, zat_thumb, zat_image, zat_ip, zat_status, iputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'members' => array(self::BELONGS_TO, 'members', 'zm_id'),//连接会员表  
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zat_id' => '附件ID',
			'zm_id' => '会员ID',
			'zat_file_name' => '文件名',
			'zat_url' => '文件地址',
			'zat_title' => '文件标题',
			'zat_description' => '文件描述',
			'zat_file_type' => '文件类型',
			'zat_file_size' => '文件大小',
			'zat_thumb' => '缩略图',
			'zat_image' => '是否图片',
			'zat_ip' => '上传IP',
			'zat_status' => '状态',
			'inputtime' => '建立时间',
			'updatetime' => '更新时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zat_id',$this->zat_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zat_file_name',$this->zat_file_name,true);
		$criteria->compare('zat_url',$this->zat_url,true);
		$criteria->compare('zat_title',$this->zat_title,true);
		$criteria->compare('zat_description',$this->zat_description,true);
		$criteria->compare('zat_file_type',$this->zat_file_type,true);
		$criteria->compare('zat_file_size',$this->zat_file_size);
		$criteria->compare('zat_thumb',$this->zat_thumb,true);
		$criteria->compare('zat_image',$this->zat_image);
		$criteria->compare('zat_ip',$this->zat_ip,true);
		$criteria->compare('zat_status',$this->zat_status);
		$criteria->compare('iputtime',$this->iputtime);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " iputtime DESC ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	//保存之前
	protected function beforeSave(){

	    if (parent::beforeSave()){	
	        if($this->isNewRecord){
	            $this->inputtime = $this->updatetime = time();
	        }else{
	        	$this->updatetime = time();
	        }	        
	        return true;
	    }else{
	        return false;
	    }
	}

	//删除之前
	protected function beforeDelete(){
		if(parent::beforeDelete()){
			Tool::delUpload($this->zat_url);//删除文件
			return true;
		}else{
			return false;
		}
	}
	
}